<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="英文名称" prop="name">
        <el-input
          v-model="form.name"
          autocomplete="off"
          :disabled="isEdit"
        ></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="displayName">
        <el-input v-model="form.displayName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="form.description" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="部门角色" prop="roleIds">
        <el-checkbox-group v-model="roleIds">
          <el-checkbox
            v-for="role in roles"
            :key="role.id"
            :label="role.id"
            @change="checkboxChangeValue"
          >
            {{ role.displayName }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit, getRoleIds } from '@/api/departmentManagement'
  import { getAll } from '@/api/roleManagement'

  export default {
    name: 'DeptManagementEdit',
    data() {
      return {
        isEdit: false,
        roles: [],
        form: {
          id: '',
        },
        roleIds: [],
        rules: {
          name: [
            { required: true, trigger: 'blur', message: '请输入英文名/拼音' },
          ],
          displayName: [
            { required: true, trigger: 'blur', message: '请输入中文名' },
          ],
          description: [
            { required: true, trigger: 'blur', message: '请输入描述' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.fetchRole()
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.isEdit = false
          this.title = '添加'
          this.roleIds = []
        } else {
          this.isEdit = true
          this.title = '编辑'
          this.form = Object.assign({}, row)
          this.roleIds = []
          this.fetchRoleIds(row.id)
        }
        this.dialogFormVisible = true
      },
      async fetchRole() {
        const { data } = await getAll()
        this.roles = data
      },
      async fetchRoleIds(id) {
        const { data } = await getRoleIds(id)
        this.form.roleIds = data
        this.roleIds = data
        console.log(data)
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.form.roleIds = this.roleIds
            const { msg } = await doEdit(this.form)
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      checkboxChangeValue(v) {
        if (v == false) {
          this.$baseMessage('不可以取消关联', 'error')
        }
      },
    },
  }
</script>
